var http = require('http');
var fs = require('fs');
var path = require('path');

var server = http.createServer(function (req, res) {
    var method = req.method;
    
    if (method === "POST") {
        var fileName = path.resolve(__dirname, 'data.txt');
        // 创建写入流
        var writeStream = fs.createWriteStream(fileName);
        
        // 管道
        req.pipe(writeStream);

        req.on('data', function (chunk) {
            console.log('chunk ', chunk.toString().length);
        })

        req.on('end', function () {
            console.log('end',num++);
            res.end('OK',num++);
        })
    }
});
server.listen(8000);